package com.hst.capacity.facade.rest;

import com.hst.capacity.application.biz.MarketBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.marketing.MarketSearchParam;
import com.hst.capacity.domain.model.request.marketing.coupon.CouponRuleParam;
import com.hst.capacity.domain.model.request.marketing.promotion.PromotionParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.infra.convertor.MarketConvertor;
import com.hst.capacity.infra.util.NumUtil;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  促销controller
 *
 * */
@RequestMapping("/market")
@RestController
public class MarketController {

    @Autowired
    private MarketBiz myBiz;

    /**
     * 获得当前加盟商的活动
     *
     * @param param
     * @return
     */
    @PostMapping("/promotion/list")
    private DefaultVO getPromotionList(@RequestBody MarketSearchParam param){
        return success(myBiz.findPromotionListByIdFranchisee(param));
    }

    /**
     * 增加或者修改活动信息
     *
     * @param param
     * @return
     */
    @PostMapping("/promotion/addOrUpd")
    private DefaultVO addOrUpdPromotion(@RequestBody PromotionParam param){
        return success(myBiz.addOrUpdPromotion(param));
    }

//    /**
//     * 禁用活动信息
//     *
//     * @param param
//     * @return
//     */
//    @PostMapping("/promotion/close")
//    private DefaultVO closePromotion(@RequestBody PromotionParam param){
//        return success(myBiz.delPromotion(param.getIdPromotion()));
//    }

    /**
     * 获得当前加盟商的优惠券规则
     *
     * @param param
     * @return
     */
    @PostMapping("/couponRule/list")
    private DefaultVO getCouponRuleList(@RequestBody MarketSearchParam param){
        return success(myBiz.findCouponRuleListByIdFranchisee(param));
    }

    /**
     * 增加或者修改优惠券规则信息
     *
     * @param param
     * @return
     */
    @PostMapping("/couponRule/addOrUpd")
    private DefaultVO addOrUpdCouponRule(@RequestBody CouponRuleParam param){
        return success(myBiz.addOrUpdCouponRule(param));
    }

//    /**
//     * 删除优惠券规则信息
//     *
//     * @param param
//     * @return
//     */
//    @PostMapping("/couponRule/del")
//    private DefaultVO delCouponRule(@RequestBody CouponRuleParam param){
//        return success(myBiz.delCouponRule(param.getIdCouponRule()));
//    }

    /**
     * 获得当前加盟商的活动的优惠券规则
     * bandKbn:0取消绑定
     * bandKbn:1绑定
     * @param param
     * @return
     */
    @PostMapping("/promotion/couponRule/list")
    private DefaultVO getCouponRuleListByPromotion(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        if (StringUtil.isBlank(idFranchisee))
            throw  new BusinessException(4001,"加盟商ID[idFranchisee]不能为空");
        String idPromotion = param.get("idPromotion");
        if (StringUtil.isBlank(idPromotion))
            throw  new BusinessException(4001,"活动ID[idPromotion]不能为空");
        int bandKbn = NumUtil.getInteger(param.get("kbn"));
        return success(myBiz.findCouponRuleListByPromotion(idFranchisee,idPromotion,bandKbn));
    }

    /**
     * 活动绑定优惠券规则
     * bandKbn:0取消绑定
     * bandKbn:1绑定
     * @param param
     * @return
     */
    @PostMapping("/promotion/couponRule/bind")
    private DefaultVO bindDriver(@RequestBody Map<String,String> param){
        String idPromotion = param.get("idPromotion");
        if (StringUtil.isBlank(idPromotion))
            throw  new BusinessException(4001,"活动ID[idPromotion]不能为空");
        String idCouponRule = param.get("idCouponRule");
        if (StringUtil.isBlank(idCouponRule))
            throw  new BusinessException(4001,"优惠规则ID[idCouponRule]不能为空");
        String bandKbn = param.get("kbn");
        if (StringUtil.isBlank(bandKbn))
            throw  new BusinessException(4001,"处理区分[kbn]不能为空");
        return success(myBiz.bindCouponRule(idPromotion,idCouponRule,NumUtil.getInteger(bandKbn)));
    }

}
